package com.example.tests;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thoughtworks.xstream.XStream;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3){
			System.out.println("Please specify parameters:<amount of test data> <file> <format>");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File exists, please remote it manually " + file);
			return;
		}
		
		List<ContactDate> contacts = generateRandomContacts(amount);
		if("csv".equals(format)){
			saveContactsToCsvFile(contacts, file);
		}else if("xml".equals(format)){
			saveContactsToXmlFile(contacts, file);
		}else {
			System.out.println("Unknown format " + format);
			return;
		}
	}

	private static void saveContactsToXmlFile(List<ContactDate> contacts, File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactDate.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<ContactDate> loadContactsFromXmlFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactDate.class);
		return (List<ContactDate>) xstream.fromXML(file);
	}

	private static void saveContactsToCsvFile(List<ContactDate> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactDate contact : contacts) {
			writer.write(contact.getFirst()+","+contact.getLast()+","+contact.getAddress()+","+
					contact.getTelephoneHome()+","+contact.getTelephoneMobile()+","+contact.getTelephoneWork()+","+
					contact.getEmail()+","+contact.getEmail2()+","+contact.getBirthDay()+","+contact.getBirthMonth()+","+
					contact.getBirthYear()+","+contact.getAddress2()+","+contact.getTelephoneHome2()+",!"+"\n");
		}
		writer.close();
	}

	public static List<ContactDate> loadGroupsFromCsvFile(File file) throws IOException {
		List<ContactDate> list = new ArrayList<ContactDate>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactDate contact = new ContactDate()
			.withFirst(part[0])
			.withLast(part[1])
			.withAddress(part[2])
			.withTelephoneHome(part[3])
			.withTelephoneMobile(part[4])
			.withTelephoneWork(part[5])
			.withEmail(part[6])
			.withEmail2(part[7])
			.withBirthDay(part[8])
			.withBirthMonth(part[9])
			.withBirthYear(part[10])
			.withAddress2(part[11])
			.withTelephoneHome2(part[12]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}
	
	public static List<ContactDate> generateRandomContacts(int amount) {
		List<ContactDate> list = new ArrayList<ContactDate>();
		
		for (int i=0; i<amount; i++){
			ContactDate contact = new ContactDate()
			.withFirst(generateRandomsString())
			.withLast(generateRandomsString())
			.withAddress(generateRandomsString())
			.withTelephoneHome("1")
			.withTelephoneMobile("1")
			.withTelephoneWork("1")
			.withEmail("test@test.ru")
			.withEmail2("test@test.ru")
			.withBirthDay("1")
			.withBirthMonth("January")
			.withBirthYear("1968")
			.withAddress2("Address 2")
			.withTelephoneHome2("Home 1");
			list.add(contact);
		}
		
		return list;
		
	}
	public static String generateRandomsString() {
		Random rmd = new Random();
		if (rmd.nextInt(3) == 0){
			return  "";
		}else {
			return "testContact" + rmd.nextInt();
		}
	}

}
